package com.sdp.wx.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_emp.entity.WxEmp;
import com.sdp.wx.db.wx_emp.service.IWxEmpService;
import com.sdp.wx.db.wx_labor_send.entity.WxLaborSend;
import com.sdp.wx.db.wx_labor_send.service.IWxLaborSendService;
import com.sdp.wx.db.wx_labor_standard.entity.WxLaborStandard;
import com.sdp.wx.db.wx_labor_standard.service.IWxLaborStandardService;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/WxLaborSendController")
public class WxLaborSendController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWxLaborSendService laborSendService;
	
	@Autowired
	private IWxLaborStandardService laborStandardService;
	
	@Autowired
	private IWxEmpService empService;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(laborSendService, WxLaborSend.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxLaborSend laborSend) {
		if(laborSend == null) {
			return Result.error("提交信息为空");
		}
		if(StringUtils.isEmpty(laborSend.getKeyId())) {
			laborSend.setCreateTime(LocalDateTime.now());
			laborSendService.save(laborSend);
		} else {
			laborSendService.updateById(laborSend);
		}
		return Result.ok(laborSend);
	}
	
	@GetMapping(value = "/del")
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		laborSendService.removeById(keyId);
		return Result.ok();
	}
	
	@PostMapping(value = "/init")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> init(@RequestBody WxLaborSend laborSend) {
		if(laborSend == null) {
			return Result.error("提交信息为空");
		}
		if (StringUtils.isEmpty(laborSend.getYearStr()) || StringUtils.isEmpty(laborSend.getQuarterStr())) {
			return Result.error("年或者季度信息为空");
		}
		Map<String, WxLaborSend> resultMap = new HashMap<>();//结果
		Map<String, Field> fieldMap = new HashMap<>();//反射设置劳保用品数量
		Map<String, WxEmp> empMap = new HashMap<>();//人员信息
		List<WxLaborStandard> standardList = laborStandardService.list();
		List<WxEmp> empList = empService.list();
		for (WxEmp wxEmp : empList) {
			empMap.put(wxEmp.getEmpNo(), wxEmp);
		}
		Field[] fields = WxLaborSend.class.getDeclaredFields();
		for (Field field : fields) {
			fieldMap.put(field.getName(), field);
		}
		Field.setAccessible(fields, true);
		
		try {
			for (WxLaborStandard wxLaborStandard : standardList) {
				for (WxEmp empInfo : empMap.values()) {
					if (wxLaborStandard.getEmpType().equals(empInfo.getEmpType())) {
						if (!resultMap.containsKey(empInfo.getEmpNo())) {
							WxLaborSend wxLaborSend = new WxLaborSend();
							wxLaborSend.setCreateTime(LocalDateTime.now());
							wxLaborSend.setOrgNo(empInfo.getOrgNo());
							wxLaborSend.setEmpNo(empInfo.getEmpNo());
							wxLaborSend.setEmpName(empInfo.getEmpName());
							wxLaborSend.setEmpType(empInfo.getEmpType());
							wxLaborSend.setYearStr(laborSend.getYearStr());
							wxLaborSend.setQuarterStr(laborSend.getQuarterStr());
							resultMap.put(wxLaborSend.getEmpNo(), wxLaborSend);
						}
						
						if (fieldMap.containsKey(wxLaborStandard.getLaborWz())) {
							Field field = fieldMap.get(wxLaborStandard.getLaborWz());
							field.set(resultMap.get(empInfo.getEmpNo()), wxLaborStandard.getWzNum());
						}
					}
				}
				
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		//先删除后添加
		QueryWrapper<WxLaborSend> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(WxLaborSend::getYearStr, laborSend.getYearStr())
			.eq(WxLaborSend::getQuarterStr, laborSend.getQuarterStr());
		laborSendService.remove(queryWrapper);
		if (resultMap.size() > 0) {
			laborSendService.saveBatch(new ArrayList<WxLaborSend>(resultMap.values()));
		}
		return Result.ok();
	}

}

